package com.dongzili.data.utils;

import org.mindrot.jbcrypt.BCrypt;


public class BCryptUtils {

    /**
     * 加密
     * @param password
     * @return
     */
    public static String encryptByBCrypt( String password) {

        return BCrypt.hashpw(password, BCrypt.gensalt());
    }

    /**
     * 校验
     * @param password
     * @param encryptStr
     * @return
     */
    public static boolean checkByBCrypt(String password, String encryptStr) {
        try{
            return BCrypt.checkpw(password, encryptStr);
        }catch (Exception e){
            return false;
        }
    }

    public static void main(String[] args) {

        String password1 = "ZXjq123!@#";
        String password2 = "ASDf4321";
        String password3 = "qwer1234!";

        String encryptStr = BCryptUtils.encryptByBCrypt(password3);
        System.out.println("加密后的密码：" + encryptStr);

        String existEncryptStr = "$2a$10$TU1LbewFZPAwPFh7SugODuDHky61XkZaQDGYorc4Cb60LyffkiSU.";
        String newPassword = "ASDf4321";
        System.out.println("是否匹配：" + BCryptUtils.checkByBCrypt(password2, existEncryptStr));

    }

}
